from fastapi import APIRouter, Query, Depends

from application.utils import get_rich_logger
from authentication.jwt import get_current_user
from domain.authentication import User
from domain.deployment import DeploymentSchema
from serve.deployments import deploy_new_serving_app, fetch_cluster_applications_info, \
    tear_down_serving_app_and_delete_record, shutdown_all_application

logger = get_rich_logger()

serve_router = APIRouter(
    prefix="/serve",
    tags=['serve']
)


@serve_router.post("/deploy")
async def create_new_serving_app(
        deployment_conf: DeploymentSchema,
        target_cluster_url: str = Query(
            default=None,
            alias="cluster",
            title="Cluster URL",
            description="Fiat Computing cluster URL."
        ),
        current_user: User = Depends(get_current_user)
):
    logger.debug(f"Serving app deployment config: {deployment_conf}")

    new_record_response = await deploy_new_serving_app(
        cluster=target_cluster_url,
        deployment_conf=deployment_conf,
        user=current_user
    )
    return new_record_response


@serve_router.get("/info")
async def get_app_deployments_info(
        target_cluster_url: str = Query(
            default=None,
            alias="cluster",
            title="Cluster URL",
            description="Fiat Computing cluster URL.",
        ),
        application_record_id: str | None = Query(
            default=None,
            alias="application-id",
            title="Application record id.",
            description="Target running application record id.",
        )
):
    metadata_records = await fetch_cluster_applications_info(
        cluster=target_cluster_url,
        record_id=application_record_id
    )
    return metadata_records


@serve_router.delete("/delete")
async def delete_serving_application(
        target_cluster_url: str = Query(
            default=None,
            alias="cluster",
            title="Cluster URL",
            description="Fiat Computing cluster URL.",
        ),
        application_name: str = Query(
            default=None,
            alias="name",
            title="Target Application name",
            description="Name of the application that user intends to delete. (Format: name-id)"
        ),
        current_user: User = Depends(get_current_user)
):
    updated_applications = await tear_down_serving_app_and_delete_record(
        cluster=target_cluster_url,
        application_name=application_name,
        user=current_user
    )
    return updated_applications


@serve_router.post("/shutdown")
async def shutdown_all_application_on_cluster(
        target_cluster_url: str = Query(
            default=None,
            alias="cluster",
            title="Cluster URL",
            description="Fiat Computing cluster URL.",
        ),
        current_user: User = Depends(get_current_user)
):
    ret = await shutdown_all_application(
        cluster=target_cluster_url,
        user=current_user
    )
    return ret
